<?php

namespace IDock\Insurance\AICS\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\AICS\AICSEnums;
use IDock\Insurance\AICS\Response\AllAICSResponse;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumRelationPrinter;
use IDock\Printer\RenewalInquiryPrinter;
use IDock\Printer\VehiclePrinter;

class RenewalInquiryResponse extends AllAICSResponse
{
    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = str_replace('&lt;','<',$body);
        $body = str_replace('&gt;','>',$body);
        $body = str_replace("'",'"',$body);
        $start = strpos($body,'<response>');
        $end = strpos($body,'</response>');
        $body = substr($body,$start + 10,intval($end - $start -10));

        if ($body) {
            libxml_disable_entity_loader(false);

            $body = simplexml_load_string($body);
            $body = $this->objectToArray($body);

            $errorCode = $body['results']['responseHead']['returnCode'] ?? '';
            $message = $body['results']['responseHead']['returnMessage'] ?? '未知错误';

            if ($errorCode !== 'E0000') {
                $this->setError($message);
                return $this;
            }
            $this->setBody($body);
        }

        return $this;
    }

    public function printerInit(): Response
    {
        $renewalInquiryPrinter = new RenewalInquiryPrinter();

        $data = $this->getBody()['results'] ?? '';
        $ciData = $data['responseBaseQ'] ?? [];
        $biData = $data['responseBaseS'] ?? [];

        $renewalInquiryPrinter->setRenewalFlg(true);

        //车辆信息
        $vehicle = $data['responseVehicleQ'] ?? [];
        $vehicle = $vehicle ?: ($data['responseVehicleS'] ?? []);
        $vehiclePrinter = new VehiclePrinter();
        $vehiclePrinter->vehicleName = $vehicle['vehicleBrand'] ?? '';
        $vehiclePrinter->vehicleModelCode = $vehicle['vehicleModel'] ?? '';
        $vehiclePrinter->power = $vehicle['power'] ?? '';
        $vehiclePrinter->vehiclePrice = $vehicle['vehiclePrice'] ?? '';
        $vehiclePrinter->vehicleSeat = $vehicle['limitLoadPerson'] ?? '';
        $vehiclePrinter->vehicleTonnage = $vehicle['tonnage'] ?? '';
        $vehiclePrinter->vehicleExhaust = $vehicle['exhaustCapacity'] ?? '';
        //车架号
        $renewalInquiryPrinter->vin = $vehicle['rackNo'] ?? '';
        //发动机号
        $renewalInquiryPrinter->engineNo = $vehicle['engineNo'] ?? '';
        $renewalInquiryPrinter->firstRegisterDate = $vehicle['registerDate'] ?? '';//初登日期
        $renewalInquiryPrinter->licenseNo = $vehicle['carMark'] ?? '';
        $renewalInquiryPrinter->ciPolicyNo = $data['responseBaseQ']['policyNo'] ?? '';
        $renewalInquiryPrinter->ciStartData = $data['responseBaseQ']['startDate'] ?? '';
        $renewalInquiryPrinter->ciEndData = $data['responseBaseQ']['endDate'] ?? '';

        $renewalInquiryPrinter->biPolicyNo = $data['responseBaseS']['policyNo'] ?? '';//上年商业险保单号
        $renewalInquiryPrinter->biStartData = $data['responseBaseS']['startDate'] ?? '';
        $renewalInquiryPrinter->biEndData = $data['responseBaseS']['endDate'] ?? '';
        //车主
        $customerInfo = $data['responseCustomerS'] ?? [];
        $customerInfo = $customerInfo ?: ($data['responseCustomerQ'] ?? '');

        //投保人
        $applicantPrinter = new PremiumRelationPrinter();
        $applicantPrinter->name = $customerInfo['appName'] ?? '';
        $applicantPrinter->mobile = $customerInfo['appmobileTel'] ?? '';
        $applicantPrinter->address = $customerInfo['appAddress'] ?? '';
        $applicantPrinter->certificateNo = $customerInfo['appCertiCode'] ?? '';
        $applicantPrinter->certificateTypeCode = array_keys(AICSEnums::$certificateTypeCode, $customerInfo['appCertiType'] ?? '')[0] ?? '';
        $applicantPrinter->email = $customerInfo['appEmail'] ?? '';
        $applicantPrinter->typeCode = $applicantPrinter->certificateTypeCode == '01' ? '01' : '03';

        $ownerPrinter = new PremiumRelationPrinter();
        $ownerPrinter->name = $customerInfo['ownercownernme'] ?? $applicantPrinter->name;
        $ownerPrinter->mobile = $customerInfo['ownercmobile'] ?? $applicantPrinter->mobile;
        $ownerPrinter->address = $customerInfo['ownercresidaddr'] ?? $applicantPrinter->address;
        $ownerPrinter->certificateNo = $customerInfo['ownerccertfcde'] ?? $applicantPrinter->certificateNo;
        $ownerPrinter->certificateTypeCode = array_keys(AICSEnums::$certificateTypeCode, $customerInfo['ownerccertfcls'] ?? '')[0] ?? $applicantPrinter->certificateTypeCode;
        $ownerPrinter->typeCode = $ownerPrinter->certificateTypeCode == '01' ? '01' : '03';

        //被保人
        $insuredPrinter = new PremiumRelationPrinter();
        $insuredPrinter->name = $customerInfo['insuredName'] ?? $applicantPrinter->name;
        $insuredPrinter->mobile = $customerInfo['insuredmobileTel'] ?? $applicantPrinter->mobile;
        $insuredPrinter->address = $customerInfo['insuredAddress'] ?? $applicantPrinter->address;
        $insuredPrinter->certificateNo = $customerInfo['insuredCertiCode'] ?? $applicantPrinter->certificateNo;
        $insuredPrinter->certificateTypeCode = array_keys(AICSEnums::$certificateTypeCode, $customerInfo['insuredCertiType'] ?? '')[0] ?? $applicantPrinter->certificateTypeCode;
        $insuredPrinter->email = $customerInfo['insuredEmail'] ?? $applicantPrinter->email;
        $insuredPrinter->typeCode = $insuredPrinter->certificateTypeCode == '01' ? '01' : '03';

        $renewalInquiryPrinter->setApplicantPrinter($applicantPrinter);
        $renewalInquiryPrinter->setInsuredPrinter($insuredPrinter);
        $renewalInquiryPrinter->setOwnerPrinter($ownerPrinter);
        $renewalInquiryPrinter->setVehiclePrinter($vehiclePrinter);

        //交强险
        $renewalInquiryPrinter->ciStartData = $ciData['startDate'] ?? '';
        $renewalInquiryPrinter->ciEndData = $ciData['endDate'] ?? '';
        $renewalInquiryPrinter->ciPolicyNo = $ciData['policyNo'] ?? '';
        $renewalInquiryPrinter->biStartData = $biData['startDate'] ?? '';
        $renewalInquiryPrinter->biEndData = $biData['endDate'] ?? '';
        $renewalInquiryPrinter->biPolicyNo = $biData['policyNo'] ?? '';

        $coverageList = $data['responseCoverageListS']['responseCoverageDataS'] ?? [];
        foreach ($coverageList as $policyItem){
            //商业险险种
            $coveragePrinter = new PremiumCoveragePrinter();
            $coverageNo = $policyItem['coverageCode'] ?? '';
            $coveragePrinter->coverageCode = array_search($coverageNo, AICSEnums::$coverageCode);

            if (!$coveragePrinter->coverageCode) {
                continue;
            }

            $coveragePrinter->coverageName = IDockEnums::$coverageCode[$coveragePrinter->coverageCode];
            $coveragePrinter->amount = $policyItem['coverageAmount'] ?? '';
            $coveragePrinter->unitAmount = round($coveragePrinter->amount / ($policyItem['coverageNumber'] ?: 1),2);
            $coveragePrinter->premium = $policyItem['coverage'] ?? '';
            $coveragePrinter->deductionRate = $policyItem['coverageRate'] ?? '';

            $renewalInquiryPrinter->setCoveragePrinterList($coveragePrinter);
        }

        $this->printer = $renewalInquiryPrinter;

        return $this;
    }
}
